<template>
	<view style="background-color: #f1f1f1;height: 100vh;">
		<view class="two">
			<u--form :model="form" ref="uForm" :rules="rules">
				<view class="two-one1">
					<view class="two-oneLeft1">单据号 :</view>
					<view class="two-oneRight1">{{YSNUM}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">产品 :</view>
					<view class="two-oneRight1">{{dataForm.ITMREF}} {{dataForm.ITMDES1}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">材检号 :</view>
					<view class="two-oneRight1">{{dataForm.YCJH}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">材质 :</view>
					<view class="two-oneRight1">{{dataForm.YCZ}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">规格 :</view>
					<view class="two-oneRight1">{{dataForm.YGG}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">申请人 :</view>
					<view class="two-oneRight1">{{dataForm.CREUSRDES}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">申请日期 :</view>
					<view class="two-oneRight1">{{dataForm.CREDAT}}</view>
				</view>
			</u--form>
			<view class="table-container" style="margin-left: -20rpx;">
				<uni-table ref="table" border stripe type=""
					emptyText="暂无更多数据" style="height: 650rpx;">
					<uni-tr>
						<uni-th align="center" width="50" :sortable="false">工序</uni-th>
						<uni-th align="center" width="100" :sortable="false">类型</uni-th>
						<uni-th align="center" width="100" :sortable="false">工序描述</uni-th>
						<uni-th align="center" width="100" :sortable="false">操作者</uni-th>
						<uni-th align="center" width="100" :sortable="false">操作日期</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in tableData" id='index' style="">
						<uni-td align="center">{{item.OPENUM}}</uni-td>
						<uni-td style="" >
							<uni-data-select style=""
								v-model="item.YLXD2"
								:localdata="range6231"
								:clear="false" disabled="">
							</uni-data-select>
						</uni-td>
						<uni-td align="center">{{item.ROODES}}</uni-td>
						<uni-td align="center">{{item.YOPEUSRDES}}</uni-td>
						<uni-td align="center">{{item.YOPEDAT}}</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box" style="">
					<uni-pagination show-icon :page-size="pageSize"
						:current="pageCurrent" :total="total"
						@change="change"/>
				</view>
			</view>
		</view>
		<div >
			<view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary"
				@click="submit(6)" >外协</u-button>
			</view>
			<view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary"
				@click="submit(7)" >确认</u-button>
			</view>
			<!-- <view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary"  v-show="this.jgqrFlg"
				@click="submit(2)" >金工确认</u-button>
			</view>
			<view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary"  v-show="this.rcqrFlg"
				@click="submit(4)" >热处理确认</u-button>
			</view>
			<view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary"  v-show="this.hbqrFlg"
				@click="submit(5)" >焊焙确认</u-button>
			</view> -->
		</div>
	</view>
</template>

<script>
	import { format } from 'date-fns';
	import * as dd from 'dingtalk-jsapi'

	export default {
		data() {
			return {
				xlqrFlg:true,
				jgqrFlg:true,
				rcqrFlg:true,
				hbqrFlg:true,
				dataForm:{},
				form: {

					},
				tableData: [],
				tableDataAll: [],
				// 每页数据量
				pageSize: 8,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				rules: {
				},
				test:false,
				YSNUM:'',
				dduData:{},
				astList1058:[],
				range6231:[]
			}
		},
		onLoad(query) {
			this.userInfo = uni.getStorageSync('userInfo');
			this.init();
			this.AST6231();
			this.test = uni.getStorageSync('test')||false;
			if(query.num){
				this.YSNUM=query.num;
				this.search();
				return;
			}
			this.scan();
		},
		mounted() {

		  },
		methods: {
			init(){
			
			},
			async AST6231() {
				let GRP1 = {
					GRP1: [{
						LANCHP: 6231
					}]
				}
				let res = await this.$api.x3.x3request("WSLISTAST",GRP1)
				// 请求成功的处理逻辑
				let resR1 = JSON.parse(res.message)
				this.astList1058= resR1.GRP1
				this.range6231=[]
				var newObject={}
				for (var i = 0; i < this.astList1058.length; i++) {
					newObject = { value: this.astList1058[i].LANNUM, text: this.astList1058[i].LANMES};
					this.range6231.push(newObject)
				}
			},
			async getDuser(){
				let GRP1 = {
						GRP1: [{
							PHONE: this.userInfo.PHONE,
					}]
				}
				let GRP1Json = JSON.stringify(GRP1)
				let res = await this.$api.x3.x3request("WSLISTYDR4",GRP1)
				this.dduData = JSON.parse(res.message)
			},
			//扫码
			scan(){
				if(this.userInfo.ID=='1'){
					this.YSNUM='YSPKX25080001';
					this.search();
					return;
				}
				var this_=this
				dd.ready(function () {
					dd.biz.util.scan({
						type: 'all', // type 为 all、qrCode、barCode，默认是all
						onSuccess: function (data) {
							//对返回的数据进行转换
							var code=data.text || data.code || data.qrCode || data.barCode
							this_.YSNUM=code;							
							uni.showToast({title: '条码信息:'+code,icon: 'success',duration: 2000});
							this_.search();
						},
						onFail: function (err) {
							console.log("scan",err)
							alert(err)
						},
					});
				});
			},
			// 分页触发
			change(e) {
				this.getData(e.current)
			},
			getData(pageCurrent, value = '') {
				this.loading = true
				this.pageCurrent = pageCurrent
				this.request({
					pageSize: this.pageSize,
					pageCurrent: pageCurrent,
					value: value,
					success: res => {
						// console.log('data', res);
						this.tableData = res.data
						this.total = res.total
						this.loading = false
					}
				})
			},
			// 伪request请求
			request(options) {
				var tableData = this.tableDataAll
				const { pageSize, pageCurrent, success, value } = options
				let total = tableData.length
				let data = tableData.filter((item, index) => {
					const idx = index - (pageCurrent - 1) * pageSize
					return idx < pageSize && idx >= 0
				})
				if (value) {
					data = []
					tableData.forEach(item => {
						if (item.name.indexOf(value) !== -1) {
							data.push(item)
						}
					})
					total = data.length
				}

				setTimeout(() => {
					typeof success === 'function' &&
						success({
							data: data,
							total: total
						})
				}, 500)
			},
			async search(){
				let GRP1 = {
					GRP1: [{
						YFLG: 3,
						YNUM: this.YSNUM,
						}]}
				let GRP1Json = JSON.stringify(GRP1)
				let res={}
				if(this.test==true){
					 res = await this.$api.x3test.x3request("WSLISTYSP8",GRP1)
				}else{
					 res = await this.$api.x3.x3request("WSLISTYSP8",GRP1)
				}
				
				let curData=JSON.parse(res.message);
				if (curData.GRP4.WOK!=0){
					uni.showToast({
					   title: curData.GRP4.WRESULT,
					   icon: 'error',
					   duration: 2000
					});
					return
				}
				this.dataForm=curData.GRP1;
				this.tableDataAll=curData.GRP2;
				if (this.dataForm.YXLCS==0){
					this.xlqrFlg=false;
				}
				if (this.dataForm.YJGCS==0){
					this.jgqrFlg=false;
				}
				if (this.dataForm.YRCCS==0){
					this.rcqrFlg=false;
				}
				if (this.dataForm.YHBCS==0){
					this.hbqrFlg=false;
				}
				this.getData(1);
			},
			async getUserInfo(){
				let GRP1 = {
						GRP1: [{
							// ID:"1",
							PHONE: this.userInfo.PHONE,
					}]
				}
				let GRP1Json = JSON.stringify(GRP1)
				let res = await this.$api.x3.x3request("WSLISTYDUR",GRP1)
				let cxsbData = JSON.parse(res.message)
			},
			async submit(q) {
				var isPerm=true
				// await this.getDuser()
				if(this.dduData.GRP1){
					if(this.dduData.GRP1.YFC==2 
					|| this.dduData.GRP1.YHB==2
					|| this.dduData.GRP1.YJG==2
					|| this.dduData.GRP1.YRC==2
					|| this.dduData.GRP1.YXL==2
					|| this.dduData.GRP1.YLH==2){
						isPerm=true
					}
				}
				const currentUser = this.userInfo.NAME; 
				// 同一个按钮，后台区分确认类型
				if (q==1){
					const authorizedUsers = ['吴超','李俊宗','曾晓琴','王勇'
				,'张茂','周亚','金安国','杨佳兰','钟建','伍祥','王浩龙'
				,'邹继威','罗东','徐阳','李露','周萌萌','胡文翀1','杨俊林','白德毅']
					if (!isPerm) {
						// 用户无权限，提示
						uni.showToast({
						    title: '您没有该按钮权限:'+currentUser,
						    icon: 'none'
						});
						return
					}
				}
				
				var this_ = this
				uni.showModal({
				  title: '提示',
				  content: '是否确认？',
				  success: function (res) {
				    if (res.confirm) {
				      this_.submitData(q);
				    }
				  }
				});
			},
			async submitData(param) {
				  let GRP1 = {
				  	GRP1: [{
							YFLAG:param,
							YNUM:this.dataForm.YNUM,
				  			YXLQUSR: this.userInfo.NAME,
							YJGQUSR: this.userInfo.NAME,
							YXLQID: this.userInfo.ID,
				  	}]
				  }
				let GRP1Json = JSON.stringify(GRP1)
				let res={}
				if(this.test==true){
					 res = await this.$api.x3test.x3request("WSGENYSPC8",GRP1)
				}else{
					 res = await this.$api.x3.x3request("WSGENYSPC8",GRP1)
				}
				let resR = JSON.parse(res.message)
				if(resR.GRP2.WOK==0){
					// 显示创建成功的消息提示
					uni.showToast({
					   title: '提交成功:'+resR.GRP2.WRESULT,
					   icon: 'success',
					   duration: 2000
					});
				let that = this
				setTimeout(function() {
					uni.switchTab({url:"/pages/managementCenter/managementCenter"})
				}, 2000);
					this.form =''
					this.dataForm=''
					this.tableData=[]
					this.tableDataAll=[]
				}else{
					uni.showToast({
					   title: '提交失败:'+resR.GRP2.WRESULT,
					   icon: 'error',
					   duration: 2000
					});
				}
			},
		}
	}
</script>

<style>
.top{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.two{
	background-color: white;
}
.two-one{
	overflow: hidden;
	margin-top: 20rpx;
	display: flex;
	justify-content: space-between;
}
.two-oneLeft{
	width: 190rpx;
	color: gray;
	text-align: right;
}
.two-oneRight{
	width: 350rpx;
	color: gray;
	text-align: left;
	margin-left: 10rpx;
}
.two-one1{
	overflow: hidden;
	margin-top: 20rpx;
	display: flex;
}
.two-oneLeft1{
	width: 200rpx;
	color: gray;
	text-align: right;
}
.two-oneRight1{
	color: gray;
	text-align: left;
	margin-left: 10rpx;
}
.there{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.four{
	height: 120rpx;
	background-color: white;
}
.four-one{
	height: 60rpx;
	display: flex;
	justify-content: space-between;
}
.four-oneLeft{
	width: 190rpx;
	line-height: 68rpx;
	color: gray;
	text-align: right;
}
.four-oneRight{
	width: 350rpx;
	line-height: 68rpx;
	color: gray;
	text-align: left;
}
.five{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.sixBox{
	height: 70rpx;
	background-color: white;
	line-height: 37rpx;
	padding-left: 40rpx;
	margin-bottom: 20rpx;
}
.four-item{
	width: 90%;
	margin-top: 16rpx;
	margin-left: 40rpx;
}
</style>
